package com.gyms.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.classic.okhttp.beans.HVMyMessageInfoBean;
import com.gyms.R;
import com.gyms.view.LinkUtils;

import java.util.List;

import utils.DateUtil;
import utils.StringUtils;

/**
 * 我的消息适配器
 */
public class MyMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<HVMyMessageInfoBean> objects;
    private LayoutInflater layoutInflater;
    private Context mContext;

    public MyMessageAdapter(Context context, List<HVMyMessageInfoBean> objects) {
        this.objects = objects;
        this.mContext=context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    private void initializeViews(HVMyMessageInfoBean mMessageBean, MessageViewHolder holder, int position) {
        if (position == objects.size() - 1) {
            holder.vLast.setVisibility(View.VISIBLE);
        } else {
            holder.vLast.setVisibility(View.GONE);
        }
        if (!StringUtils.isNull(mMessageBean)) {
            holder.tvTitle.setText(mMessageBean.getTitle());
            holder.tvTime.setText(DateUtil.getSimpleDateFomeS(mMessageBean.getCreateTime()));
            holder.tvMessage.setText(mMessageBean.content);
            LinkUtils.interceptHyperLink(mContext,holder.tvMessage);
            LinkUtils.removeHyperLinkUnderline(holder.tvMessage);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View mView = layoutInflater.inflate(R.layout.item_mymessage, parent, false);
        return new MessageViewHolder(mView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        initializeViews(objects.get(position), (MessageViewHolder) holder, position);
    }

    @Override
    public int getItemCount() {
        return null==objects?0:objects.size();
    }

    private class MessageViewHolder extends RecyclerView.ViewHolder{
        private TextView tvTitle;
        private TextView tvTime;
        private TextView tvMessage;
        private View vLast;
        MessageViewHolder(View itemView) {
            super(itemView);
            tvTitle= (TextView) itemView.findViewById(R.id.tv_title);
            tvTime= (TextView) itemView.findViewById(R.id.tv_time);
            tvMessage= (TextView) itemView.findViewById(R.id.tv_message);
            vLast=  itemView.findViewById(R.id.v_last);
        }

    }
}
